package com.dubbo.provider.serviceimpl;

import com.dubbo.api.HotWordService;
import com.dubbo.api.PaperService;
import com.dubbo.api.model.*;
import com.dubbo.provider.entity.Browser;
import com.dubbo.provider.entity.HotWord;
import com.dubbo.provider.entity.Paper;
import com.dubbo.provider.entity.PaperHWord;
import com.dubbo.provider.mapper.*;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import java.math.BigDecimal;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;


@Service
public class HotWordServiceImpl implements HotWordService {

    @Autowired
    private KeywordMapper keywordMapper;
    @Autowired
    private BrowserMapper browserMapper;
    @Autowired
    private HotWordMapper hotWordMapper;
    @Autowired
    private PaperHWordMapper paperHWordMapper;
    @Autowired
    private  PaperMapper paperMapper;

    @Override
    public KeywordModel countKW(String KWord) {
        //原始词频统计
        int count = keywordMapper.countKeywords(KWord);
        KeywordModel keywordModel = new KeywordModel();
        keywordModel.setCount(count);
        return keywordModel;
    }

    @Override
    public void userReadPaper(BrowserModel browserModel) {
        //校验,用户登录？

        //model->browser
        Browser browser = convertBroswerFromBroswerModel(browserModel);
        //入库
        browserMapper.insert(browser);
        return ;
    }

    @Override
    public void Update(HotWordModel hotWordModel) {
        //更新热词
        HotWord hotWord = new HotWord();
        //model-》转化为
        hotWord = convertHotWordFromHotWordModel(hotWordModel);
        hotWordMapper.updateByPrimaryKeySelective(hotWord);
    }

    @Override
    public PaperHWordModel selectByPid(Integer id) {
       PaperHWord paperHWord =paperHWordMapper.selectBypId(id);
        return this.convertPaperHWordModelFromPaperHWord(paperHWord);
    }

    @Override
    public void insertPW(PaperHWordModel paperHWordModel) {
        PaperHWord paperHWord = new PaperHWord();
        paperHWord = converPaperHWordFromPaperHWordModel(paperHWordModel);
        paperHWordMapper.insertSelective(paperHWord);
    }

    @Override
    public void insertHW(HotWordModel hotWordModel) {
            HotWord hotWord = new HotWord();
            hotWord = convertHotWordFromHotWordModel(hotWordModel);
            hotWordMapper.insertSelective(hotWord);
    }

    @Override
    public List<String> getKWord(Integer pId) {
        List<String> kWordList = keywordMapper.getKWord(pId);
        return kWordList;
    }

    @Override
    public HotWordModel selectByHWord(String hWord) {
        HotWord hotWord = hotWordMapper.selectByHWord(hWord);
        return this.convertHotWordFromHotWordModel(hotWord);
    }

    @Override
    public void updateCurrent(HotWordModel hotWordModel) {
        HotWord hotWord = new HotWord();
        hotWord = convertHotWordFromHotWordModel(hotWordModel);
        hotWordMapper.updateCurrent(hotWord);
    }

    @Override
    public List<HotWordModel> rankBefore() throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //得到当前时间，用于计算时间差
        Date date = new Date();
        String nowtime = df.format(date);
        Date d1 = df.parse(nowtime);
        //得到历史词频
        double B;
        List<HotWordModel> hotWordModels = listHW();
        for (int i = 0;i<hotWordModels.size();i++) {
            B = (hotWordModels.get(i).getHotCurrent() +
                    hotWordModels.get(i).getAvgTp() *
                            hotWordModels.get(i).getAvgR()) /
                    ((hotWordModels.get(i).getHotCurrent() +
                            hotWordModels.get(i).getHotHistory()) +
                            hotWordModels.get(i).getAvgTp());
            hotWordModels.get(i).setB(B);
            //查到pid通过pw表，pid-》paper-》time
            int pid = paperHWordMapper.selectByhId(hotWordModels.get(i).getHotId());
            String hDate = paperMapper.hDate(pid);
            Date d2 = df.parse(hDate);//历史词频时间
            long  diff = d1.getTime() - d2.getTime();// 这样得到的差值是微秒级别
            long days = diff / (1000 * 60 * 60 * 24);//天
            long hours = (diff - days * (1000 * 60 * 60 * 24))//时间差，小时
                    / (1000 * 60 * 60);
            double log = Math.log(((double)hotWordModels.get(i).getHotCurrent()+1)/(hotWordModels.get(i).getHotHistory()+1))/
                    (days*24+hours);
            hotWordModels.get(i).setA(log);
            if(hotWordModels.get(i).getHotCurrent()>15||
               hotWordModels.get(i).getB()>0.5&&hotWordModels.get(i).getHotCurrent()>8||
               hotWordModels.get(i).getA()>0.3&&hotWordModels.get(i).getHotCurrent()>8
             ){
               HotWord hotWord = new HotWord();
               hotWord.setHotValue(hotWordModels.get(i).getA()*0.3+
                       hotWordModels.get(i).getB()*0.7);
               hotWord.setHotId(hotWordModels.get(i).getHotId());
               hotWordMapper.updateByPrimaryKeySelective(hotWord);
            }
        }
            return hotWordModels;
    }

    @Override
    public List<HotWordModel> listHW() {
        List<HotWord> hotWordList = hotWordMapper.listHW();
        List<HotWordModel> hotWordModelList = hotWordList.stream().map(hotWord -> {
            return this.convertHotWordFromHotWordModel(hotWord);
        }).collect(Collectors.toList());
        return hotWordModelList;
    }

    @Override
    public List<HotWordModel> sort(List<HotWordModel> hotWordModels) {
        Collections.sort(hotWordModels, new Comparator<HotWordModel>() {
            @Override
            public int compare(HotWordModel o1, HotWordModel o2) {
                return o2.getHotValue().compareTo(o1.getHotValue());
            }
        });
       return hotWordModels;
    }

    private HotWordModel convertHotWordFromHotWordModel(HotWord hotWord){
        if (hotWord == null){
            return null;
        }
        HotWordModel hotWordModel = new HotWordModel();
        BeanUtils.copyProperties(hotWord,hotWordModel);
        //转换为BigDecimal
        hotWordModel.setHotValue(new BigDecimal(hotWord.getHotValue()));
        return hotWordModel;
    }

    private PaperHWord converPaperHWordFromPaperHWordModel(PaperHWordModel paperHWordModel){
        if(paperHWordModel == null){
            return null;
        }
        PaperHWord paperHWord = new PaperHWord();
        BeanUtils.copyProperties(paperHWordModel,paperHWord);
        return paperHWord;
    }

    private PaperHWordModel convertPaperHWordModelFromPaperHWord(PaperHWord paperHWord){
        if(paperHWord == null){
            return null;
        }
        PaperHWordModel paperHWordModel = new PaperHWordModel();
        BeanUtils.copyProperties(paperHWord,paperHWordModel);
        return paperHWordModel;
    }

    private HotWord convertHotWordFromHotWordModel(HotWordModel hotWordModel){
        if(hotWordModel == null)
        {
            return  null;
        }
        HotWord hotWord = new HotWord();
        BeanUtils.copyProperties(hotWordModel,hotWord);
        return hotWord;
    }

    private Browser convertBroswerFromBroswerModel(BrowserModel browserModel){
        if(browserModel == null){
            return null;
        }
        Browser browser = new Browser();
        BeanUtils.copyProperties(browserModel,browser);
        return browser;

    }
}
